﻿using UnityEngine;
using System;
using GoogleMobileAds;
using GoogleMobileAds.Api;
using Soomla;

namespace Soomla.Store.Example {

public class watchVideo : MonoBehaviour {
	public Transform double_point;
	public Transform chiffre0;
	public Transform chiffre1;
	public Transform chiffre2;
	public Transform chiffre3;
	public Transform chiffre4;
	public Transform chiffre5;
	public Transform chiffre6;
	public Transform chiffre7;
	public Transform chiffre8;
	public Transform chiffre9;
	public Sprite spriteAppuye;
	public Sprite sprite;
	private Transform prefab_choisi;
	private int nombre_chiffre;
	private double taille_totale;
	private Vector3 position_depart;
	private double taille_chiffre;

	private string phpController = "http://jolan.konig.free.fr/folder/phpcontroller.php";
	private WWW posgetwww;
	private string posurl;
	private Transform[] intermediate;
	private int tempsDebutChrono;
	private int tempsFinChrono;
	private double tempsEcoule;
	private int heureRestante;
	private int nombre_heure;
	private int nombre_minute;
	void Start()
	{
		Time.timeScale = 1;
		posurl  = phpController + "?action=getservertime";
		posgetwww = new WWW(posurl);
		intermediate=new Transform[5];
		tempsEcoule = 0;
		tempsFinChrono=PlayerPrefs.GetInt("lastTimeWatchVideo")+10800;
		heureRestante = 0;
		taille_chiffre=(chiffre0.GetComponent<SpriteRenderer>().sprite.bounds.size.x+0.01)*this.transform.parent.localScale.x;
		position_depart=this.transform.position;

		}
	// Use this for initialization
	void OnMouseDown () {
		this.transform.GetComponent<SpriteRenderer> ().sprite = spriteAppuye;
	}
	void OnMouseUp()
	{
		if (GetServerTime () - PlayerPrefs.GetInt ("lastTimeWatchVideo") >= 3600) {
			ShowInterstitial ();
		} else {
			this.transform.GetComponent<SpriteRenderer>().sprite=null;
			tempsDebutChrono=GetServerTime ();
		}

		}
	
	private void ShowInterstitial()
	{
		if (chargeVideo.interstitial.IsLoaded())
		{
			chargeVideo.interstitial.Show();
			this.transform.GetComponent<SpriteRenderer>().sprite=null;
			StoreInventory.GiveItem("currency_coins",100);
			PlayerPrefs.SetInt ("lastTimeWatchVideo",GetServerTime ());

		}
		else
		{
			print("Interstitial is not ready yet.");
		}
	}

	void Update()
	{

		if (this.transform.GetComponent<SpriteRenderer> ().sprite == null) {
			tempsEcoule+=Time.deltaTime;
			heureRestante=tempsFinChrono-(tempsDebutChrono+(int)tempsEcoule);
			if(heureRestante<0)
			{
				heureRestante=0;
			}
			afficherChrono(heureRestante);
				}
	}


	
	int GetServerTime() {

		//yield posgetwww;
		if(posgetwww.error!=null) {
			// error... epic FAIL!
			print("There was an error saving the level: " + posgetwww.error);
			return 0;
		} else {
			// no errors... WIN!
			return System.Convert.ToInt32 (posgetwww.text);
		}
	}
	void afficherChrono(int hr)
	{
		position_depart=this.transform.position;
		position_depart -= new Vector3(6,0,0)*this.transform.localScale.x;
		nombre_heure = nombreHeure(hr);
		nombre_minute=(hr-nombre_heure*3600)/60;
		for (int i=4; i>=0; i--) {
						if (intermediate [i] != null) {
								GameObject.Destroy (intermediate [i].gameObject);
						}
				}
		intermediate[0]=Transform.Instantiate(chiffre0,position_depart,Quaternion.identity) as Transform;
		position_depart.x+=(float)taille_chiffre;
		intermediate[0].parent=this.transform.parent;
		intermediate[0].localScale=new Vector3(1,1,1);

		prefab_choisi=choisirPrefab(nombre_heure);
		intermediate[1]=Transform.Instantiate(prefab_choisi,position_depart,Quaternion.identity) as Transform;
		position_depart.x+=(float)taille_chiffre;
		intermediate[1].parent=this.transform.parent;
		intermediate[1].localScale=new Vector3(1,1,1);

		intermediate[2]=Transform.Instantiate(double_point,position_depart,Quaternion.identity) as Transform;
		position_depart.x+=(float)taille_chiffre;
		intermediate[2].parent=this.transform.parent;
		intermediate[2].localScale=new Vector3(1,1,1);

		prefab_choisi=choisirPrefab(nombre_minute/10);
		intermediate[3]=Transform.Instantiate(prefab_choisi,position_depart,Quaternion.identity) as Transform;
		position_depart.x+=(float)taille_chiffre;
		intermediate[3].parent=this.transform.parent;
		intermediate[3].localScale=new Vector3(1,1,1);

		prefab_choisi=choisirPrefab(nombre_minute-((nombre_minute/10)*10));
		intermediate[4]=Transform.Instantiate(prefab_choisi,position_depart,Quaternion.identity) as Transform;
		position_depart.x+=(float)taille_chiffre;
		intermediate[4].parent=this.transform.parent;
		intermediate[4].localScale=new Vector3(1,1,1);
	}
	int nombreHeure(int hr)
	{
		return hr/3600;

	}

Transform choisirPrefab(int chiffre)
{
	if(chiffre==0)
	{
		return chiffre0;
	}
	else if(chiffre==1)
	{
		return chiffre1;
	}
	else if(chiffre==2)
	{
		return chiffre2;
	}
	else if(chiffre==3)
	{
		return chiffre3;
	}
	else if(chiffre==4)
	{
		return chiffre4;
	}
	else if(chiffre==5)
	{
		return chiffre5;
	}
	else if(chiffre==6)
	{
		return chiffre6;
	}
	else if(chiffre==7)
	{
		return chiffre7;
	}
	else if(chiffre==8)
	{
		return chiffre8;
	}
	else if(chiffre==9)
	{
		return chiffre9;
	}
	else
	{
		return null;
	}
	
}

}

}
